﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.Reflection;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using System.Net;
using Sector7.Framework.Utility;

namespace Sector7.Restaurant.APs.ServiceBus.Utility
{
    /// <summary>
    /// WCF服务组管理工具
    /// </summary>
    public class WcfHostUtil
    {
        #region Inner members
        /// <summary>
        /// WCF服务列表
        /// </summary>
        private static List<ServiceHost> _hosts = new List<ServiceHost>();

        /// <summary>
        /// 启动成功服务个数
        /// </summary>
        private static int _countSuccess = 0;

        /// <summary>
        /// 启动失败服务个数
        /// </summary>
        private static int _countFailure = 0;
        #endregion

        #region Public functions
        /// <summary>
        /// 加载并打开所有WCF服务
        /// </summary>
        public static void OpenAllServices()
        {
            _countFailure = 0;
            _countSuccess = 0;

            Console.WriteLine("ServiceBus starting all service:");

            Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            ServiceModelSectionGroup serviceGroup = (ServiceModelSectionGroup)config.GetSectionGroup("system.serviceModel");
            foreach (ServiceElement service in serviceGroup.Services.Services)
            {
                Type serviceType = Type.GetType(service.Name);

                if (serviceType == null)
                {
                    Logger.WriteLine(LogSeverityType.ERR, "Service Type [{0}] is invalid.", service.Name);
                    Console.WriteLine("ERR: Service Type [{0}] is invalid.", service.Name);

                    ++_countFailure;
                }
                else
                {
                    OpenService(serviceType);
                }
            }

            Console.WriteLine();
            Console.WriteLine("SUCCESS: [{0}], FAILURE: [{1}], TOTAL: [{2}]",
                _countSuccess,
                _countFailure,
                _countSuccess + _countFailure);
            Console.WriteLine();
        }

        /// <summary>
        /// 关闭所有WCF服务
        /// </summary>
        public static void CloseAllServices()
        {
            foreach (ServiceHost host in _hosts)
            {
                host.Close();
            }
        }
        #endregion

        #region Private functions
        /// <summary>
        /// 打开单个WCF服务
        /// </summary>
        /// <param name="serviceType">WCF服务类型</param>
        private static void OpenService(Type serviceType)
        {
            ServiceHost host = new ServiceHost(serviceType);
            
            try
            {
                host.Open();
                Console.WriteLine("\t{0} startup Success.", host.Description.Name);
                Logger.WriteLine(LogSeverityType.INF, "{0} startup Success.", host.Description.Name);

                ++_countSuccess;

                _hosts.Add(host);               
            }
            catch (Exception e)
            {
                Logger.WriteLine(LogSeverityType.ERR, "{0} startup Failure", host.Description.Name);
                Console.WriteLine("\t{0} startup Failure.", host.Description.Name);
                Logger.WriteLine(LogSeverityType.ERR, e);

                ++_countFailure;
            }
        }
        #endregion
    }
}
